package com.bt.contronller;

import com.bt.config.User;
import com.bt.sevice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping("/user")
@RestController
public class UserController {
	
	@Autowired
	private UserService uService;
	
	//查询单个用户
	@GetMapping("/getUser/{id}")
	@ResponseBody
	public User selectById(@PathVariable("id") Integer id){
		User user=uService.selectUserById(id);
		if(user!=null)
			return user;
		else
			return null;
	}
	
	//查询所有用户
	@GetMapping("/getUserList")
	public List<User> getList(){
		List<User> ulist=uService.getUserList();
		if(ulist!=null)
			return ulist;
		else
		    return null;
	}
	
	//查询用户
	@PostMapping("/addUser")
	public String addUser(User user){
		int num=uService.addUser(user);
		if(num>0)
			return "success";
		else
			return "error";
	}
	
	//删除用户
	@DeleteMapping("/delUser/{id}")
	public String delUser(@PathVariable Integer id){
		int num=uService.delUser(id);
		if(num>0)
			return "success";
		else
			return "error";
	}
	
	//修改用户
	@PutMapping("/updateUser")
	public String updateUser(@RequestBody  User user){
		int num=uService.updateUser(user);
		if(num>0)
			return "success";
		else
			return "error";
	}
}